package online.xuping.thousandsOfWorlds.sd;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.SneakyThrows;
import online.xuping.common.IdUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * 文字变成图片
 */
public class Text2Img {

    @SneakyThrows
    public static void main(String[] args) {
        String text2 = "(worst quality, low quality:1.4),logo,signature,watermark,text,(worst quality, low quality:1.4),logo,signature,watermark,text,(worst quality:2),NSFW,NSFW,logo,text,blurry,low quality,bad anatomy,sketches,lowres,normal quality,username,out of focus,bad proportions,cropped,watermark,signature,worstquality,grayscale,monochrome,fused fingers,extra arms,skin spots,deformed,Multiple people,(AS-YoungV2-Neg:1.3),mutated,acnes,acnes,mutated hands,too many fingers,extra legs,bad feet,cross-eyed,mutated hands and fingers,malformed hands,poorly drawn hands,missing limb,malformed limbs,disfigured,floating limbs,ugly,mutation,(AS-Adult-Neg:1.3),bad body,long body,(fat:1.2),long neck,skin blemishes,poorly drawn face,disconnected limbs,skin blemishes,missing fingers,bad hands,";
        SdWordsDomain sdWordsDomain = new SdWordsDomain();
        sdWordsDomain.setNegative_prompt(text2);
        sdWordsDomain.size(512,1024);
        sdWordsDomain.setSamplerIndex(SamplerEnum.UNIPC.getName());
        sdWordsDomain.setSamplerName(SamplerEnum.UNIPC.getName());
        sdWordsDomain.setSteps(30);
        file2Image("C:\\Users\\Administrator\\AppData\\Roaming\\JetBrains\\IntelliJIdea2023.2\\scratches\\魔法内容\\灵仆-2023-11-02 - 1720055571846008832.txt", sdWordsDomain, "E:\\本地素材库\\4");
    }
    public static void file2Image(String filePath, SdWordsDomain sdWordsDomain, String savePath) {
        FileReader fileReader = new FileReader(filePath);
        fileReader.readLines().stream().forEach(it->{
            String a[] = it.split(",");
            String name = "";
            // 获取开头第一个字符串作为名字
            if (a!=null && a.length >=0) {
                name = a[0];
            }
            sdWordsDomain.setPrompt(it);
            try {
                text2Image( sdWordsDomain, savePath + "\\[" + name + "]" + IdUtils.newId2() + ".png");
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        });
    }

    @SneakyThrows
    public static   void  text2Image(String text,String text2, String path) {
        SdWordsDomain sdWordsDomain = new SdWordsDomain();
        sdWordsDomain.setPrompt(text);
        sdWordsDomain.setNegative_prompt(text2);
        sdWordsDomain.setHeight(1024);
        sdWordsDomain.setWidth(512);
        sdWordsDomain.setSamplerIndex("UniPC");
        sdWordsDomain.setSamplerName("UniPC");
        sdWordsDomain.setSteps(50);
        text2Image(sdWordsDomain, path);
    }


    @SneakyThrows
    public static   void  text2Image(SdWordsDomain sdWordsDomain, String path) {

        String json = JSONUtil.toJsonStr(sdWordsDomain);
        String result2 = HttpRequest.post("http://localhost:7777/sdapi/v1/txt2img")
                .body(json)
                .execute().body();

        JSONObject re = JSONUtil.parseObj(result2);
        JSONArray images = re.getJSONArray("images");
        File output = new File(path);
        BufferedImage bufferedImage= ImgUtil.toImage(images.get(0).toString());
        ImageIO.write(bufferedImage, "png", output);
    }
}
